package com.xiaobai.dao.user;

import com.sun.istack.internal.Nullable;
import com.xiaobai.dao.BaseDao;
import com.xiaobai.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author 小白学CS
 * @Version 1.0
 */
public class UserDaoImpl implements UserDao {

    @Override
    public User getLoginUser(@Nullable Connection conn, String userCode) throws SQLException {
        @Nullable PreparedStatement pstm = null;
        @Nullable ResultSet rs = null;
        @Nullable User user = null;
        //如果连接不为空，则执行sql查询
        if (conn != null) {
            /*通过登录名做条件查询表中的所有人，select只得到一条数据，以下使用预编译的sql*/
            String sql = "select * from smbms_user where userCode=?";
            Object[] params = {userCode};

            rs=BaseDao.execute(conn,pstm,rs,sql,params);
            /*如果rs还有下一个数据，则user数据从rs中遍历查询user表的数据*/
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUserName(rs.getString("userName"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(rs.getInt("userRole"));
                user.setCreateBy(rs.getInt("createBy"));
                user.setCreateDate(rs.getDate("createDate"));
                user.setModifyBy(rs.getInt("modifyBy"));
                user.setModifyDate(rs.getDate("modifyDate"));
            }
            /*关闭rs查询，第一的参数为conn连接池为null不需要关闭*/
            BaseDao.closeResource(null,pstm,rs);
        }
        return user;
    }
}
